package com;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class GridPanel extends JPanel{
	private static final long serialVersionUID = 1L;
	private Image whiteqz;
	private Image blackqz;
	private Dimension size;
	private ChessArray chessArray;
	private Map<Color, Image> image;
	private boolean isStart;
	private boolean opponentStart;
	private boolean isMy;
	private Boolean win;
	private Color chessColor;
	private ChessPanel chessPanel;
	
	
	public Boolean getWin() {
		return win;
	}
	public void setWin(Boolean win) {
		this.win = win;
	}
	public Color getChessColor() {
		return chessColor;
	}
	public void setChessColor(Color chessColor) {
		this.chessColor = chessColor;
	}
	public boolean isStart() {
		return isStart;
	}
	public void setStart(boolean isStart) {
		this.isStart = isStart;
		if(chessPanel==null) chessPanel=(ChessPanel) getParent();
	}
	public boolean isMy() {
		return isMy;
	}
	public void setMy(boolean isMy) {
		this.isMy = isMy;
	}
	public boolean isOpponentStart() {
		return opponentStart;
	}
	public void setOpponentStart(boolean opponentStart) {
		this.opponentStart = opponentStart;
	}
	public GridPanel() {
		win=null;
		isStart=false;
		opponentStart=false;
		isMy=false;
		setOpaque(false);
		setCursor(new Cursor(Cursor.HAND_CURSOR));
		whiteqz=new ImageIcon(getClass().getResource("/res/whiteChessman.png")).getImage();
		blackqz=new ImageIcon(getClass().getResource("/res/blackChessman.png")).getImage();
		image=new HashMap<Color, Image>();
		image.put(Color.white, whiteqz);
		image.put(Color.black, blackqz);
		chessArray=new ChessArray();
		chessArray.addPropChangeListener(new PropertyChangeListener() {
			@Override
			public void propertyChange(PropertyChangeEvent evt) {
				repaint();
			}
		});
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(!isStart||!opponentStart||!isMy) return;
				int x=e.getX(),y=e.getY();
				int chessw=size.width/15,chessh=size.height/15;
				int j=(y-size.height%15/2)/chessh;
				int i=(x-size.width%15/2)/chessw;
				if(chessArray.getChess(i, j)==null)
				    {
					   MainFrame mf=MainFrame.getInstence();
					   Chess chess=new Chess(i, j,chessColor);
					   mf.send(mf.getSocket(), chess);
					   chessArray.setChess(chess);
					   setMy(false);
				    }
			}
		});
	}	
	@Override
	public void paint(Graphics g1) {
		Graphics2D g = (Graphics2D) g1;
		super.paint(g); // 调用父类的绘图方法
		if(chessPanel!=null) chessPanel.setCurrentPlayer(isMy);
		drawPanel(g); // 调用绘制棋盘的方法
		size=new Dimension(getWidth(), getHeight());
		int chessw=size.width/15,chessh=size.height/15;
		for(int i=0;i<15;i++)
			for(int j=0;j<15;j++){
				Color chess=chessArray.getChess(i, j);
				if(chess!=null)
				   g.drawImage(image.get(chess), i*chessw+size.width%15/2, j*chessh+size.height%15/2, chessw, chessh, this);				
			}
        if(!chessArray.chessStack.isEmpty()&&isStart){
			Chess qz=chessArray.chessStack.getFirst();
		    int lasti=qz.getI(),lastj=qz.getJ();
		    g.setColor(Color.red);
		    g.drawRect(lasti*chessw+size.width%15/2, lastj*chessh+size.height%15/2, chessw, chessh);
        }    
		if(!isStart||!opponentStart){
			
			String mess="";
			if(!isStart)
			     mess="请点击开始...";
			else if(!opponentStart)
				mess="等待对方开始...";
			drawString(g, mess, Color.black,48,1f);			
		}
		if(!isStart&&win!=null){
			if(win)
			   drawString(g, "您胜利了", Color.red,72,0.6f);
			else
			   drawString(g, "您失败了", Color.green,72,0.6f);
		}		
	}
	private void drawString(Graphics2D g,String mess,Color color,int size,float alpha){
		Composite composite=g.getComposite();
		Font font=new Font("隶书", Font.ITALIC|Font.BOLD, size);
		g.setFont(font);
		g.setColor(color);
		g.setComposite(AlphaComposite.SrcOver.derive(alpha));
		FontRenderContext frc=g.getFontRenderContext();
		Rectangle2D r2=font.getStringBounds(mess, frc);
		g.drawString(mess, (int)(getWidth()-r2.getWidth())/2, (int)(getHeight()-r2.getHeight())/2);
		g.setComposite(composite);
	}
	private void drawPanel(Graphics2D g) {
		Composite composite = g.getComposite(); // 备份合成规则
		Color color = g.getColor(); // 备份前景颜色
		g.setComposite(AlphaComposite.SrcOver.derive(0.8f));// 设置透明合成
		g.setColor(new Color(0xAABBAA)); // 设置前景白色
		g.fill3DRect(0, 0, getWidth(), getHeight(), true); // 绘制半透明的矩形
		g.setComposite(composite); // 恢复合成规则
		g.setColor(color); // 恢复原来前景色
		int w = getWidth(); // 棋盘宽度
		int h = getHeight(); // 棋盘高度
		int chessW = w / 15, chessH = h / 15; // 棋子宽度和高度
		int left = chessW / 2 + (w % 15) / 2; // 棋盘左边界
		int right = left + chessW * 14; // 棋盘右边界
		int top = chessH / 2 + (h % 15) / 2; // 棋盘上边界
		int bottom = top + chessH * 14; // 棋盘下边界
		for (int i = 0; i < 15; i++) {
			// 画每条横线
			g.drawLine(left, top + (i * chessH), right, top + (i * chessH));
		}
		for (int i = 0; i < 15; i++) {
			// 画每条竖线
			g.drawLine(left + (i * chessW), top, left + (i * chessW), bottom);
		}
	}

}
